package com.pengtu.gsj.controller.checker;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.pengtu.gsj.controller.base.CheckBaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.entity.check.CheckProject;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.check.CheckProjectService;
import com.pengtu.gsj.service.system.SysmenuManagerService;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

@Controller
@RequestMapping("provinceCheck")
public class ProvinceCheckController extends CheckBaseController {
	
	@Autowired
	private CheckProjectService checkProjectService;
	@Autowired
	private SysmenuManagerService sysmenuManagerService;
 
	
	@ModelAttribute
	public void loadEntity(String id, Model model){
		CheckProject checkProject ;
		if (StringUtils.isNotEmpty(id)) {
			checkProject = checkProjectService.getCheckProject(id);
		}else {
			checkProject = new CheckProject();
		}
		model.addAttribute("checkProject", checkProject);
		model.addAttribute("parentId", SpringMvcUtils.getParameter("parentId"));
		model.addAttribute("left", SpringMvcUtils.getParameter("left"));
		model.addAttribute("type", SpringMvcUtils.getParameter("type"));
	}
	
	/**
	 * 
	 * showList:
	 * 适用:跳转到国省列表页面
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("showList")
	public String showList() {
		return "check/provinceCheck";
	}
	
	/**
	 * 
	 * getCheckProjectList:
	 * 适用:获取国省数据列表
	 * @param page  接受page相关的数据
	 * @param request
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value="list", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Page<CheckProject> getCheckProjectList(Page<CheckProject> page, HttpServletRequest request, Model model){
		String msg = SpringMvcUtils.getParameter("msg");
		if (StringUtils.isNotEmpty(msg)) {
			model.addAttribute("msg", msg);
		}
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = checkProjectService.getCheckProjects(page, queryFilter);
		return page;
	}
	
	/**
	 * 
	 * showOrInput:
	 * 适用:跳转到新增国省项目页面或者是修改国检项目信息页面
	 * @param checkProject
	 * @param model
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("input")
	@Token(save = true)
	public String showOrInput(@ModelAttribute CheckProject checkProject, Model model) {
		//获取所有日常监管菜单中的业务模块菜单
		List<Sysmenu> menuList =sysmenuManagerService.getcheckSysmenus();
		model.addAttribute("menuList", menuList);
		return "check/checkProject_input";
	}
	
	/**
	 * 
	 * checkProjectName:
	 * 适用: ajax判断国检项目名称是否已经存在
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value="check",produces = "application/json; charset=utf-8")
	public Boolean checkProjectName(){
		String oldName = SpringMvcUtils.getParameter("oldName");
		String projectName = SpringMvcUtils.getParameter("projectName");
		Boolean exist = checkProjectService.isprojectNameUnique(projectName, oldName);
		return exist;
	}
	
	/**
	 * 
	 * saveCheckProject:
	 * 适用:保存国检项目信息
	 * @param checkProject
	 * @param attributes
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("save")
	@Token(remove = true)
	public String saveCheckProject(@ModelAttribute CheckProject checkProject,RedirectAttributes attributes)throws Exception{
		checkProjectService.save(checkProject);
		attributes.addFlashAttribute("msg", "项目保存/更新成功！");
		attributes.addAttribute("parentId",	SpringMvcUtils.getParameter("parentId"));
		attributes.addAttribute("left", SpringMvcUtils.getParameter("left"));
		return "redirect:/province/showList.do";
	}
	
	@RequestMapping("release")
	@ResponseBody
	public boolean releaseCheckProject(@ModelAttribute CheckProject checkProject)throws Exception{
		checkProject.setStatus("1"); //设置成发布状态 可能还需要将涉及到的检查表设置成不可编辑状态
		checkProjectService.save(checkProject);
		return true;
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public boolean deleteCheckData(@ModelAttribute CheckProject checkProject) {
		checkProjectService.delete(checkProject);
		return true;
	}
}
